$(function() {
    $('#quick-search').quickSearch({
        callback: function(search, opt) {
            $(core.events.SearchTable).triggerHandler('set-config', [
                {search: search.length ? search : null, page: null},
                true
            ]);
        }
    });
    $('.navigation .button').click(function() {
        $(this).parents('#searcher-wrapper').find('#items-wrapper').triggerHandler('next-page');
    });
    $('.filter-select').filter();
    $('.sort-switcher').sort_switch();

});

(function($) {
    $.namespace = function(namespace) {
        var parts = namespace.split('.');
        var head = window;
        var part = parts[0];
        for (var i = 0; i < parts.length; i++,part = parts[i]) {
            if (typeof(head[part]) === 'undefined') {
                head = (head[part] = {});
            } else {
                head = head[part];
            }
        }
        return head;
    }
})(jQuery);

(function($) {
    $.fn.defaultText = function() {
        $.each(this, function() {
            this.__defaultText = $(this).val();
            $(this).bind('focus', function() {
                if (this.__defaultText === $(this).val()) {
                    $(this).val(null);
                }
            });
            $(this).bind('blur', function() {
                if ($(this).val() === '') {
                    $(this).val(this.__defaultText);
                }
            });
        });
    };
})(jQuery);

$(function() {
    $(core.events).triggerHandler('page-load');
});

(function($) {
    var reg_o = /^\[([^0-9]\w*)\]/,
        reg_v = /^(\w+)/;

    function parse(str, val) {
        var split_val, ret_val;
        if (str.length === 0) return val;
        if (reg_o.test(str)) return (ret_val = {})[(split_val = str.split(reg_o))[1]] = parse(split_val[2], val),ret_val;
        if (reg_v.test(str)) return (ret_val = {})[(split_val = str.split(reg_v))[1]] = parse(split_val[2], val),ret_val;
    }

    jQuery.unparam = function(str) {
        if(str.length === 0) { return {}; }
        var parts = str.split('&'),
            key_value,
            result = {};
        $.each(parts, function() {
            var key = decodeURIComponent((key_value = this.split('='))[0]),
                val = decodeURIComponent(key_value[1]);
            $.extend(true, result, parse(key, val));
        });
        return result;
    };
})(jQuery);

(function($) {
    var ns = $.namespace('core.events.Address');
    $.address.change(function(_event_) {
        $(ns).triggerHandler('change', [_event_]);
    });
    $.address.internalChange(function(_event_) {
        $(ns).triggerHandler('internal-change', [_event_]);
    });
    $.address.externalChange(function(_event_) {
        $(ns).triggerHandler('external-change', [_event_]);
    });
})(jQuery);